﻿#include "FITKOFSnappyHexMeshDriver.h"

#include "FITK_Kernel/FITKAppFramework/FITKAbstractProgramDriver.h"
#include "FITK_Kernel/FITKAppFramework/FITKAbstractCommandRunner.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"
#include <QFile>
#include <QFileInfo>
#include <QDebug>
#include <QProcess>
#include <QDir>

namespace FoamDriver
{
    int FITKOFSnappyHexMeshDriver::getProgramType()
    {
        return 1;
    }

    QString FITKOFSnappyHexMeshDriver::getProgramName()
    {
        return "snappyHexMesh";
    }

    void FITKOFSnappyHexMeshDriver::start()
    {
         AppFrame::FITKMessageNormal("Start meshing");
        //输入参数判空
        if (!_inputInfo) return;

        // 获取snappyHexMesh启动参数
        QStringList snappyHexMeshArguments = _inputInfo->args();
        QString aa =_inputInfo->namestl;
         QStringList GeoFileNameList =_inputInfo->namestllist;
        qDebug() << "snappyHexMeshArguments:" << snappyHexMeshArguments;

        // 获取监控的路径
        QStringList monitorPaths = _inputInfo->getMonitorPath();

        qDebug() << "monitorPaths:" << monitorPaths;

        QString snappyHexMeshCommond = "snappyHexMesh " + snappyHexMeshArguments.join(" ");

        qDebug() << "snappyHexMeshCommond:" << snappyHexMeshCommond;

        // 判断监控路径是否存在
        int indexError = -1;
        if (!_commandRunner->isExistDictionary(monitorPaths, indexError))
        {
            QString str;
            if (indexError != -1)
                str = QString("Not Exist Dictionary Path : %1").arg(monitorPaths[indexError]);
            sendMessage(3, str);
        }

        //需要gmshexe的路径，geo路径，输出文件路径 snappyHexMeshArguments是路径case
        //C:\Users\Administrator\Desktop\flow-git\version---fast-sim\output\WorkDir\case\constant\geometry  +"\\gmsh.exe"
        QString  Path=snappyHexMeshArguments.at(1);
        QString  geometryfile=Path+"\\constant\\geometry";
        QString  gmshExePath=Path+"\\gmsh";//gmshexe路径
        // 使用 QFileInfo 获取上一级路径
        QFileInfo fileInfo(Path);
        QString parentDir = fileInfo.dir().absolutePath()+"\\gmsh";
        //对双色币的stl文件执行网格划分打到

//如果是双色币
        if (aa.contains("Two")) {
       for (int i = 0; i < GeoFileNameList.size(); ++i) {

            QString fileName = GeoFileNameList[i];

            QString  geoFile=geometryfile+"\\"+GeoFileNameList[i]+".Geo";//geo文件

            QString  outputFile=geometryfile+"\\"+GeoFileNameList[i]+".vtk";//生成结果文件路径

            QString command1 = "\"" + parentDir + " " + geoFile +
                               " -format vtk "
                               "-bin " // 添加此选项以生成二进制文件
                               "-o " + outputFile +
                               " -3\"";

            qDebug() << "Command Line:" << command1;
            std::string stdCommand1 = command1.toStdString();
            //int result = std::system(stdCommand1.c_str());
            runGmshCommand(parentDir, geoFile, outputFile);
         }

} else {
//除了双色币之外全是单个模型
             QString fileName = aa;

             QString  geoFile=geometryfile+"\\"+aa+".Geo";//geo文件

             QString  outputFile=geometryfile+"\\"+aa+".vtk";//生成结果文件路径

             QString command1 = "\"" + parentDir + " " + geoFile +
                                " -format vtk "
                                "-bin " // 添加此选项以生成二进制文件
                                "-o " + outputFile +
                                " -3\"";

             qDebug() << "Command Line:" << command1;
             std::string stdCommand1 = command1.toStdString();
             //int result = std::system(stdCommand1.c_str());
             runGmshCommand(parentDir, geoFile, outputFile);

}
        AppFrame::FITKMessageNormal("Meshing successful");




        QString command22 = "ping www.google.com -n 1";
        // 执行命令
        connect(_commandRunner, SIGNAL(taskFinishedSig(FITKThreadTask*)), this, SLOT(threadFinishedSlot()), Qt::UniqueConnection);
        _commandRunner->setExecuteCommand(command22);
        _commandRunner->push2ThreadPool();

    }
    void FITKOFSnappyHexMeshDriver::runGmshCommand(const QString &parentDir, const QString &geoFile, const QString &outputFile) {
        QProcess process;

        // 构造命令和参数
        QStringList arguments;
        arguments << geoFile
                  << "-format" << "vtk"
                  << "-bin" // 二进制格式
                  << "-o" << outputFile
                  << "-3";  // 3D网格生成


        // 启动进程
       // process.start(parentDir, arguments);
        process.setProgram(parentDir);
        process.setArguments(arguments);
        process.start();
        if (!process.waitForFinished(-1)) { // -1 表示无限等待
            qDebug() << "Gmsh command execution failed.";
            qDebug() << "Error Output:" << process.readAllStandardError();
            qDebug() << "Standard Output:" << process.readAllStandardOutput();
        }


        // 捕获输出
        QString output = process.readAllStandardOutput();
        QString errorOutput = process.readAllStandardError();

        // 打印结果
//        qDebug() << "Gmsh Standard Output:\n" << output;
//        qDebug() << "Gmsh Standard Error:\n" << errorOutput;
          AppFrame::FITKMessageNormal("Meshing:"+output+"Meshing:"+errorOutput);
    }

    void FITKOFSnappyHexMeshDriver::stop()
    {
        _commandRunner->killProcess(_commandRunner->getID());
    }
}
